package com.wave2.shadow.webservices;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wave2.shadow.dao.EventsDAO;
import com.wave2.shadow.persistence.EventsList;
import com.wave2.shadow.misc.Constants;

@Controller("eventWSController")
@RequestMapping(value="/event/")
public class WSEventsController {
	
	private static final String MY_VIEW_NAME = "showevents";		//TODO: this should be injected in.
	
	@Autowired
	private EventsDAO eventsDAO;									//TODO: This should really autowire to a service that calls the DAO. Fix this.
	
	
	//default case is to get default list.
	@RequestMapping(method = RequestMethod.GET, produces = Constants.MEDIA_TYPE_XML)
	public @ResponseBody EventsList getDefaultEventList(){
	
		return eventsDAO.getDefaultList();
		
	}
	
	//search for a event by organizer name.
	@RequestMapping(value="/name/{name}", method = RequestMethod.GET, produces = Constants.MEDIA_TYPE_JSON)
	public @ResponseBody EventsList getEvent(@PathVariable String name) {
 
		return eventsDAO.getEventsByName(name);
		
	}
	
	//Delete an event.
	@RequestMapping(method = RequestMethod.DELETE)
	public ModelAndView deleteEvent(@RequestParam("eventId") String id) {
		
		eventsDAO.deleteEvent(Integer.parseInt(id));
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/events.htm");

		return mv;
		
	} 
	
	//Add an event
	@RequestMapping(method = RequestMethod.POST)
	public void addEvent(@RequestParam("eventId") String id) {
		//TODO: To be implemented
	}
	
	//Update an event
	@RequestMapping(method = RequestMethod.PUT)
	public void updateEvent(@RequestParam("eventId") String id) {
		//TODO: To be implemented
	}
	
	
}
